﻿using MediatR;
using W.Device.API.Application.Command;
using W.Device.Repository.Interface;

namespace W.Device.API.Application.CommandHandlder
{
    public class DeviceDeleteCommandHandlder:IRequestHandler<DeviceDeleteCommand,int>
    {
        private readonly IDeviceRepository deviceRepository;

        public DeviceDeleteCommandHandlder(IDeviceRepository deviceRepository)
        {
            this.deviceRepository = deviceRepository;
        }

        public async Task<int> Handle(DeviceDeleteCommand request, CancellationToken cancellationToken)
        {
            var list = await deviceRepository.GetAsync(w=>w.DeviceId==request.DeviceId);
            list.FirstOrDefault().IsDelete = true;
            list.FirstOrDefault().DeleteTime = DateTime.Now;
            return await deviceRepository.Update(list.FirstOrDefault());
        }
    }
}
